#pragma once
#ifndef _MSG_QUEUE
#define _MSG_QUEUE

#define LOKI_CLASS_LEVEL_THREADING
#include <Loki\Singleton.h>
class CMsgData
{
public:
	CMsgData()
	{
		lpData = NULL;
		nSize = 0;
	}

	CMsgData( CONST CMsgData& data )
		: lpData( data.lpData )
		, nSize( data.nSize )
		, m_socket( data.m_socket )
	{
	}

	explicit CMsgData( LPVOID pData, size_t size, HANDLE sock )
	{
		lpData = new char[size+4];
		memcpy( lpData, pData, size );
		*(int*)((char*)lpData+size) = 0;
		nSize = size;
		m_socket = sock;
	}

	~CMsgData()
	{
		nSize = 0;
		if( lpData )
		{
			delete lpData;
			lpData = NULL;
		}
	}

	LPVOID			lpData;
	size_t			nSize;
	HANDLE			m_socket;
};

template< class context >
class CMsgQueue
{
	friend struct Loki::CreateStatic< CMsgQueue >;
private:
	CMsgQueue(void){}
	~CMsgQueue(void){}

public:
	bool get( context& obj )
	{
		CCriticalLock lock( m_lock );
		if( m_msg_queue.empty() ) return false;
		obj = m_msg_queue.front();
		m_msg_queue.pop();
		return true;
	}

	void put( context obj )
	{
		CCriticalLock lock( m_lock );
		m_msg_queue.push( obj );
	}

private:
	typedef std::queue< context >	CInnerQueueType;

	CCritical		m_lock;
	CInnerQueueType m_msg_queue;
};

typedef Loki::SingletonHolder< CMsgQueue< CMsgData* >, Loki::CreateStatic >	CMsgQueueMng;
#endif // _MSG_QUEUE